package com.nopay.dxdwphone.custom_view;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;

import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;

import java.util.List;


public class BottomSheetDialog {

    private QMUIBottomSheet qmuiBottomSheet;
    private Activity mActivity;
    private List<String> mList;

    public QMUIBottomSheet initSimpleBottomsheet(String title, boolean withMark, Activity activity, List<String> list, int selectedPosistion, final OnBottomSheetListener listener){
        this.mActivity = activity;
        this.mList = list;

        return initSimpleBottomSheetList1(true,
                true,
                false,
                title,
                mList.size(),
                false,
                withMark,
                selectedPosistion,
                listener);

    }

    private QMUIBottomSheet initSimpleBottomSheetList1(boolean gravityCenter,
                                                       boolean addCancelBtn,
                                                       boolean withIcon,
                                                       CharSequence title,
                                                       int itemCount,
                                                       boolean allowDragDismiss,
                                                       boolean withMark,
                                                       int selectedPosistion,
                                                       final OnBottomSheetListener listener) {
        QMUIBottomSheet.BottomListSheetBuilder builder = new QMUIBottomSheet.BottomListSheetBuilder(mActivity);
        builder.setGravityCenter(gravityCenter)
                .setSkinManager(QMUISkinManager.defaultInstance(mActivity))
                .setTitle(title)
                .setAddCancelBtn(addCancelBtn)
                .setAllowDrag(allowDragDismiss)
                .setNeedRightMark(withMark)
                .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                    @Override
                    public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                        dialog.dismiss();
                        listener.onSelected(position);
                    }
                });

        for (int i = 0; i < mList.size(); i++) {
            if (selectedPosistion != -1){
                if (i == selectedPosistion){
                    builder.setCheckedIndex(i);
                }
            }

            builder.addItem(mList.get(i));
        }
        try {
            qmuiBottomSheet = builder.build();
            qmuiBottomSheet.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    listener.onCancel();
                }
            });
            qmuiBottomSheet.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    listener.onDismiss();
                }
            });
            return qmuiBottomSheet;
        }catch (Exception e){

        }
        return null;

    }

    public void show(){
        if (qmuiBottomSheet != null){
            if (qmuiBottomSheet.isShowing()){
                qmuiBottomSheet.show();
            }
        }
    }

    public interface OnBottomSheetListener {

        void onSelected(int position);
        void onCancel();
        void onDismiss();
    }

}
